package handlers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"xiao-hong-shu-sys/models"
	"xiao-hong-shu-sys/pkg"
)

func List(c *gin.Context) {
	tel := c.Query("tel")
	password := c.Query("password")
	user_id := c.Query("user_id")
	var user models.User
	err := models.DB.Where("tel=? and password=?", tel, pkg.Md5(password)).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "登录失败",
		})
		return
	}
	token := pkg.TokenHandler(user_id)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": token,
	})
}

func Lists(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var red []models.Red
	var count int64
	models.DB.Model(red).Count(&count)
	err := models.DB.Offset(offset).Limit(size).Find(&red).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"red":   red,
		},
	})
	return

}
